﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_YUESHENBAOInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        CustomGridView CustomGridView1;
        DropDownList DropDownList1, DropDownList2;
        Label lblQS;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            if (!base.Page.IsPostBack)
            {
                Bind();
                QSDataDLL();
                if (string.IsNullOrEmpty(HttpContext.Current.Request["year"]) && string.IsNullOrEmpty(HttpContext.Current.Request["month"]))
                {
                    string temp1 = System.DateTime.Now.Year.ToString();
                    string temp2 = System.DateTime.Now.Month.ToString();
                    string tzdz = "ZS_YUESHENBAOInfo.aspx?qs=" + base.Page.Request["qs"];
                    if (!string.IsNullOrEmpty(temp1))
                    {
                        tzdz += "" + "&year=" + temp1;
                    }
                    if (!string.IsNullOrEmpty(temp2))
                    {
                        tzdz += "" + "&month=" + temp2;
                    }
                    base.Page.Response.Redirect(tzdz);
                }
            }
            CustomGridView1.OnLoadData+=new EventHandler(CustomGridView1_OnLoadData);
            CustomGridView1.RowDeleting += new GridViewDeleteEventHandler(CustomGridView1_RowDeleting);
            Button btnsearch = (Button)FindControl("btnSeach");
            btnsearch.Click += new EventHandler(Go_Search);
            Button btnAdd = (Button)FindControl("btnAdd");
            btnAdd.Click += new EventHandler(btnAdd_Click);
        }
        protected void InitControl() {
            CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            DropDownList1 = (DropDownList)FindControl("DropDownList1");
            DropDownList2 = (DropDownList)FindControl("DropDownList2");
            lblQS = (Label)FindControl("lblQS");
        }
        protected void Go_Search(object sender, EventArgs e)
        {
            getDZ();
            Bind();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            base.Page.Response.Redirect("ZS_YUESHENBAOAdd.aspx?qs=" + HttpContext.Current.Request["qs"] + "&year=" + DropDownList1.SelectedValue + "&month=" + DropDownList2.SelectedValue);
        }
        protected void Bind()
        {
            string strQS = base.Page.Request["QS"];
            lblQS.Text = _EluZSDataProvider.GetParamsOne("区属", strQS);
            if (!string.IsNullOrEmpty(base.Page.Request["year"]))
            {
                DropDownList1.SelectedValue = base.Page.Request["year"];
            }
            if (!string.IsNullOrEmpty(base.Page.Request["month"]))
            {
                DropDownList2.SelectedValue = base.Page.Request["month"];
            }
            DataTable dt = _EluZSDataProvider.GetYBBXX(GetStrWhere());//获取人员数据
            CustomGridView1.DataSource = dt;
            CustomGridView1.RecordCount = dt.Rows.Count;
            CustomGridView1.PageSize = 10;
            CustomGridView1.DataKeyNames = new string[] { "ID" };
            CustomGridView1.DataBind();
            
        }
        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected void CustomGridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string strid = CustomGridView1.DataKeys[e.RowIndex].Value.ToString();
            bool del = _EluZSDataProvider.DeleteXmByID("ZS_YBBTJ", "id", strid);
            if (del)
            {
                Bind();
                Antu.Utilities.UtilitiesPage.AlertDialog("删除成功！", "");
            }
        }
        protected string GetStrWhere() {
            string strwhere = string.Empty;
            if (!string.IsNullOrEmpty(base.Page.Request["year"]))
            {
                strwhere += " and YEAR='" + base.Page.Request["year"] + "'";
            }
            else
            {
                string year = System.DateTime.Now.Year.ToString();
                strwhere += " and YEAR='" + year + "'";
            }
            if (!string.IsNullOrEmpty(base.Page.Request["month"]))
            {
                strwhere += " and MONTH='" + base.Page.Request["month"] + "'";
            }
            else
            {
                strwhere += " and MONTH='1'";
            }
            if (!string.IsNullOrEmpty(base.Page.Request["qs"]))
            {
                strwhere += @" and qs='" + base.Page.Request["qs"] + "'";
            }
            return strwhere;
        }

        protected void getDZ()
        {
            string temp1 = DropDownList1.SelectedValue;//年
            string temp2 = DropDownList2.SelectedValue;//月
            string tzdz = "ZS_YUESHENBAOInfo.aspx?qs=" + base.Page.Request["qs"];
            if (!string.IsNullOrEmpty(temp1))
            {
                tzdz += "" + "&year=" + temp1;
            }
            if (!string.IsNullOrEmpty(temp2))
            {
                tzdz += "" + "&month=" + temp2;
            }
            base.Page.Response.Redirect(tzdz);
        }
        protected void QSDataDLL()
        {
            string strYEAR = System.DateTime.Now.Year.ToString();
            string strSSYEAR = System.Configuration.ConfigurationManager.AppSettings["SSYEAR"].ToString();
            int ss = Convert.ToInt32(strSSYEAR);
            List<string> lyear = new List<string>();
            int y1 = Convert.ToInt32(strYEAR);//2013
            int ye1 = y1 + 1;//2014
            int yc = ye1 - ss;//4
            if (yc > 0)
            {
                for (int i = 0; i < yc; i++) 
                {
                    if (!lyear.Contains(strSSYEAR)) { lyear.Add(strSSYEAR); }
                    lyear.Add((2010 + i + 1).ToString());
                }
            }
            else 
            {
                lyear.Add(strSSYEAR);
            }
            DropDownList1.DataSource = lyear;
            DropDownList1.DataBind();
        }
    }
}


